IF EXISTS (SELECT * FROM sys.objects WHERE [name] = N'zDT_PM10100FIDMSS' AND [type] = 'TR')
BEGIN
      DROP TRIGGER [dbo].[zDT_PM10100FIDMSS];
END;
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

/****** Object:  Trigger [dbo].[zDT_PM10100FIDMSS]    Script Date: 11/9/2023 10:19:46 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE TRIGGER [dbo].[zDT_PM10100FIDMSS]
ON [dbo].[PM10100]
   AFTER INSERT
AS 
BEGIN
	SET NOCOUNT ON;
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ANSI_WARNINGS ON

	DECLARE @BatchNumber VARCHAR(15),
			@VoucherNumber VARCHAR(21) = NULL,
			@VendorID VARCHAR(15),
			@DocType INT,
			@CompanySeg VARCHAR(15),
			@BranchSeg VARCHAR(15),
			@PAY_DIST_TYPE SMALLINT = 2,
			@BranchID char(50),
			@InvoiceNumber char(21),
			@MSS_OrderNo char(50)

	IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'MSS10000')
	BEGIN	
		
		IF EXISTS (SELECT 1 FROM inserted i join PM10000 p on i.VCHRNMBR = p.VCHRNMBR AND i.CNTRLTYP = 0
				JOIN SY00500 s ON p.BACHNUMB=s.BACHNUMB AND s.BCHSOURC=p.BCHSOURC
				WHERE (s.BCHCOMNT = 'Batch Created by Fidesic' or (LOWER(s.BACHNUMB) like '%fidesic%' 
					AND s.BCHCOMNT not like 'Batch Created by Fidesic MSS')) AND s.BCHSOURC='PM_Trxent' 
					AND ((p.DOCTYPE = 1 and i.DEBITAMT > 0) OR (p.DOCTYPE = 5 and i.CRDTAMNT > 0)))
					AND NOT EXISTS(SELECT * FROM inserted i join MSS10000 m on m.DOCNUMBR = i.VCHRNMBR AND i.CNTRLTYP = 0)
		BEGIN
			SELECT @BatchNumber = BACHNUMB, @VoucherNumber = i.VCHRNMBR, 
				@InvoiceNumber = p.DOCNUMBR,
				@DocType = p.DOCTYPE
			FROM inserted i 
			JOIN PM10000 p on i.VCHRNMBR = p.VCHRNMBR
			AND i.CNTRLTYP = 0

			/* Find the first GL from Fidesic and figure out the value of the segment that is assigned to the facility */
			SELECT top 1 @CompanySeg = g.ACTNUMBR_1, @BranchSeg = g.ACTNUMBR_2
			FROM inserted ig
			JOIN GL00105 g on ig.DSTINDX = g.ACTINDX
			AND ig.CNTRLTYP = 0

			select @BranchID = b.BranchID from MoversSuite2.dbo.Branch b where b.CompanyGLCode = @CompanySeg and b.GLCode = @BranchSeg
	
			INSERT INTO dbo.MSS10000
           (SERIES,DOCTYPE,DOCNUMBR,MSS_OrdPriKey,MSS_ATPriKey,MSS_OrderNo,MSS_CustomerNumber2, 
			MSS_Authority,MSS_NANumber,MSS_Driver,MSS_SalesPerson,MSS_ItemCode,MSS_BranchCode,
			MSS_InvoiceNumber,MSS_InvoiceDate,MSS_ItemCodeDesc,MSS_ServiceCode,MSS_ShipperName)
			SELECT 
				  SERIES = 4,
				  DOCTYPE = @DocType,
				  DOCNUMBR = @VoucherNumber,
				  MSS_OrdPriKey = ISNULL((SELECT TOP 1 PriKey FROM MoversSuite2..Orders WHERE OrderNo = @MSS_OrderNo AND Archived = 0 ),0),
				  MSS_ATPriKey = 0,
				  MSS_OrderNo = ISNULL(@MSS_OrderNo,''),
				  MSS_CustomerNumber2 = '',
				  MSS_Authority = '',
				  MSS_NANumber = '',
				  MSS_Driver = '',
				  MSS_SalesPerson = '',
				  MSS_ItemCode = 0,
				  MSS_BranchCode = ISNULL(@BranchID,''),
				  MSS_InvoiceNumber = @InvoiceNumber,
				  MSS_InvoiceDate = '1/1/1900',
				  MSS_ItemCodeDesc = '',
				  MSS_ServiceCode = '',
				  MSS_ShipperName = ''
		END
		ELSE IF EXISTS (SELECT 1 FROM inserted i join PM10000 p ON i.VCHRNMBR = p.VCHRNMBR AND i.CNTRLTYP = 0
				JOIN SY00500 s ON p.BACHNUMB=s.BACHNUMB AND s.BCHSOURC=p.BCHSOURC
				WHERE (s.BCHCOMNT like 'Batch Created by Fidesic%' or LOWER(s.BACHNUMB) like '%fidesic%') AND s.BCHSOURC='PM_Trxent')
			AND EXISTS (SELECT 1 FROM inserted t WHERE t.DISTTYPE = @PAY_DIST_TYPE AND t.CNTRLTYP = 0)
		BEGIN
			
			SELECT	@BatchNumber = pp.BACHNUMB, 
					@VoucherNumber = p.VCHRNMBR, 
					@DocType = pp.DOCTYPE,
					@VendorID = p.VENDORID
			FROM inserted p
			JOIN PM10000 pp ON pp.VCHNUMWK = p.VCHRNMBR
			WHERE p.DISTTYPE = @PAY_DIST_TYPE
			AND p.CNTRLTYP = 0

			DECLARE @outAccountNumber varchar(66),
					@inBranchID INT

			/* Find the first GL from Fidesic and figure out the value of the segment that is assigned to the facility */
			select @inBranchID = b.GLCPriKey
			FROM inserted i 
			JOIN MSS10000 m 
				on m.DOCNUMBR = i.VCHRNMBR AND i.CNTRLTYP = 0
			JOIN MoversSuite2.dbo.Branch b 
				on b.BranchID = m.MSS_BranchCode

			exec [MoversSuite2].[dbo].[spGPGetGLARAPDefault] 
				@inBranchID = @inBranchID,
				@inTransactionTypeName = 'AP',
				@outAccountNumber = @outAccountNumber output

			UPDATE p
			SET DSTINDX = g.ACTINDX
			FROM inserted i
			JOIN PM10100 p on p.DEX_ROW_ID = i.DEX_ROW_ID
			JOIN GL00105 g on g.ACTNUMST = @outAccountNumber
			AND i.CNTRLTYP = 0

		END
	END

	SET NOCOUNT OFF
	SET CONCAT_NULL_YIELDS_NULL OFF
	SET ANSI_WARNINGS OFF
END
GO
